package m.jclecai.com.dialog;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.utils.ScreenUtil;

/**
 * Created by ZD on 2017/4/20.
 * 彩票注数目
 */

public class MyCaiPiaoDialog extends BaseDialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.ll_title)
    FrameLayout llTitle;
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.llBody)
    LinearLayout llBody;
    @BindView(R.id.sv_container)
    ScrollView svContainer;
    @BindView(R.id.tv_again)
    TextView tvAgain;
    @BindView(R.id.tv_sure)
    TextView tvSure;

    List<List<String>> mDatas;
    //固定生日数字的下标 用来设置背景
    List<Map<Integer,Integer>> birthIndex;
    //蓝球个数
    private int blueCount  = 0;

    public MyCaiPiaoDialog(Context context, List<List<String>> datas, int blueCount) {
        super(context);
        this.mDatas = datas;
        this.blueCount = blueCount;
        initUI();
    }

    public MyCaiPiaoDialog(Context context, List<List<String>> datas, int blueCount, List<Map<Integer,Integer>> birthIndex) {
        super(context);
        this.mDatas = datas;
        this.birthIndex = birthIndex;
        this.blueCount = blueCount;
        initUI();
    }

    /**
     * 重新显示数据
     * @param datas
     */
    public void setData( List<List<String>> datas,int blueCount){
        this.mDatas = datas;
        this.blueCount = blueCount;
        llBody.removeAllViews();
        loadData();
    }

    void initUI() {
        setContentView(R.layout.dialog_caipiao);
        if (MyApplication.status==0){
            tvAgain.setVisibility(View.VISIBLE);
            tvSure.setVisibility(View.VISIBLE);
        }else {
            tvAgain.setVisibility(View.GONE);
            tvSure.setVisibility(View.GONE);
        }
        loadData();
    }

    void loadData(){
        int radius = ScreenUtil.dip2px(getContext(),22);
        int margin = ScreenUtil.dip2px(getContext(),2);
        for (int i=0;i<mDatas.size();i++){
            final View inflate = View.inflate(getContext(), R.layout.item_dialog_caipiao, null);
            ImageView close = (ImageView) inflate.findViewById(R.id.iv_close);
            close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setEnabled(false);
                    Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.slide_out_to_right);
                    inflate.startAnimation(animation);
                    animation.setFillAfter(true);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            inflate.setVisibility(View.GONE);
                            inflate.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    llBody.removeView(inflate);
                                }
                            },100);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                }
            });
            LinearLayout llNumBody = (LinearLayout) inflate.findViewById(R.id.llNumBody);
            llNumBody.setTag(new Gson().toJson(mDatas.get(i)));
            addNum(llNumBody,mDatas.get(i),radius,margin,i);
            llBody.addView(inflate);
            Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_from_right);
            animation.setDuration(600);
            inflate.setAnimation(animation);
        }
    }

    /**
     * 设置号码
     * @param v
     * @param num
     * @param radius
     */
    void addNum(ViewGroup v,List<String> num,int radius,int margin,int index){
        Map<Integer,Integer> bgMap = null;
        if(birthIndex != null)
            bgMap = birthIndex.get(index);
        for (int i=0;i<num.size();i++){
            System.out.print(num.get(i));
            System.out.print(" ");
            TextView tv = new TextView(getContext());
            //添加view需要提前 否则后面的LayoutParams = null
            v.addView(tv);
            tv.setText(num.get(i).trim());
            tv.setGravity(Gravity.CENTER);
            //切换红蓝球的背景
            int color = ContextCompat.getColor(getContext(),R.color.color_f54646);
            if(i >= num.size() - blueCount)
                color = ContextCompat.getColor(getContext(),R.color.color_39f);
            if(bgMap != null&&bgMap.get(i) !=null){
                color = ContextCompat.getColor(getContext(),R.color.color_3bbc56);
            }
            if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
                tv.setBackground(getGradientDrawable(color));
            else
                tv.setBackgroundDrawable(getGradientDrawable(color));
            tv.setTextColor(color);
            //球之间的间距
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(radius,radius);
            if(i > 0)
                lp.leftMargin = margin;
            tv.setLayoutParams(lp);
        }
        System.out.println(" ");
    }

    /**
     * 号码背景
     * @param color
     * @return
     */
    GradientDrawable getGradientDrawable(int color){
        int strokeWidth = 2; // 3dp 边框宽度
        int roundRadius = 15; // 8dp 圆角半径

        GradientDrawable gd = new GradientDrawable();//创建drawable
        gd.setShape(GradientDrawable.OVAL);
//        gd.setColor(color);
        gd.setCornerRadius(roundRadius);
        gd.setStroke(strokeWidth, color);
        return gd;
    }
    
    public List<List<String>> getDatas(){
        List<List<String>> datas= new ArrayList<>();
        if(llBody!=null && llBody.getChildCount() > 0){
            int childCount = llBody.getChildCount();
            for (int i=0;i<childCount;i++){
                View childAt = llBody.getChildAt(i);
                Object tag = childAt.findViewById(R.id.llNumBody).getTag();
                // json转为带泛型的list  
                List<String> retList = new Gson().fromJson(tag.toString(),new TypeToken<List<String>>(){}.getType());
                datas.add(retList);
            }
        }
        return datas;
    }

    @OnClick(R.id.tv_again)
    void again(){
        if(this.listener != null)
            this.listener.again();
    }

    @OnClick(R.id.tv_sure)
    void sure(){
        if(this.listener != null)
            this.listener.sure(getDatas());
    }

    @OnClick(R.id.iv_close)
    void close(){
        dismiss();
    }



    public interface onSelectListsner{
        void again();//再来一次
        void sure(List<List<String>> datas);//立即投注
    }

    private onSelectListsner listener;

    public void setOnSelectListener(onSelectListsner listener){
        this.listener = listener;
    }
}
